clear all;

sig=2.0;

colr='k:';
colr_pp='g--';
colr_cov='r-';
colr_reprof='b-.';

jz=3;
ja=12;

amin=0;
amax=0.08;

q0=0.961538;
delta=0.8775;
freq=1.0;
averz=0.5;
averz=averz*freq;
debtfactor=1.0/((1.0-q0*delta)*freq);
amax=(amax/averz)*debtfactor;

% .. benchmark data ..
dirpath = ['welfare\welf_def000.dat'];
mydata = importdata(dirpath, ' ', 1);
AA=mydata.data;
% filtering
row_idx=(AA(:,2)==jz); 
A_filt=AA(row_idx,:); % filtering
zz= A_filt(:,4);
aa= A_filt(:,3);
%aa=aa/.5;
aa=(aa/averz)*debtfactor;
val= A_filt(:,5);
val=((1-sig)*val).^(1/(1-sig));
%val=(val./val)-1;
HH= A_filt(:,6);


% .. nomktaccess - pari passu ..
dirpath = ['counterfact\mktaccess\welfare\welf_def000.dat'];
mydata = importdata(dirpath, ' ', 1);
AA=mydata.data;
% filtering
row_idx=(AA(:,2)==jz); 
A_filt=AA(row_idx,:); % filtering
aa_pp= A_filt(:,3);
%aa_pp=aa_pp/.5;
aa_pp=(aa_pp/averz)*debtfactor;
val_pp= A_filt(:,5);
val_pp=((1-sig)*val_pp).^(1/(1-sig));
val_pp=(val_pp./val)-1;
HH_pp= A_filt(:,6);
HHpc_pp=HH_pp./HH-1;

% .. covenant ..
dirpath = ['counterfact\covenant\welfare\welf_def000.dat'];
mydata = importdata(dirpath, ' ', 1);
AA=mydata.data;
% filtering
row_idx=(AA(:,2)==jz); 
A_filt=AA(row_idx,:); % filtering
aa_cov= A_filt(:,3);
%aa_cov=aa_cov/.5;
aa_cov=(aa_cov/averz)*debtfactor;
val_cov= A_filt(:,5);
val_cov=((1-sig)*val_cov).^(1/(1-sig));
val_cov=(val_cov./val)-1;
HH_cov= A_filt(:,6);
HHpc_cov=HH_cov./HH-1;

% .. reprofiling ..
dirpath = ['counterfact\reprof\kap060\welfare\welf_def000.dat'];
mydata = importdata(dirpath, ' ', 1);
AA=mydata.data;
% filtering
row_idx=(AA(:,2)==jz); 
A_filt=AA(row_idx,:); % filtering
aa_reprof= A_filt(:,3);
%aa_reprof=aa_reprof/.5;
aa_reprof=(aa_reprof/averz)*debtfactor;
val_reprof= A_filt(:,5);
val_reprof=((1-sig)*val_reprof).^(1/(1-sig));
val_reprof=(val_reprof./val)-1;
HH_reprof= A_filt(:,6);
HHpc_reprof=HH_reprof./HH-1;

val=(val./val)-1;

figure(4);
plot(aa,val,colr,'LineWidth',1.5);
hold on;
plot(aa_pp,val_pp,colr_pp,'LineWidth',1.5);
hold on;
plot(aa_cov,val_cov,colr_cov,'LineWidth',1.5);
hold on;
plot(aa_reprof,val_reprof,colr_reprof,'LineWidth',1.5);
ylabel('val');
%plot(a,aprF,'g-.','LineWidth',2.0);
legend ('val', 'val_{pp}','val_{cov}','val_{reprof}','Location','northeast' );
ylim([-.01 .01]);
xlim([amin amax]);
title(['jz'  num2str(jz)  ]);
grid on;
%hold off;

print -djpeg slides\welf_z3.jpg


figure(5);
% plot(aa,HH,colr,'LineWidth',1.5);
% hold on;
plot(aa_pp,HHpc_pp,colr_pp,'LineWidth',1.5);
hold on;
plot(aa_cov,HHpc_cov,colr_cov,'LineWidth',1.5);
hold on;
plot(aa_reprof,HHpc_reprof,colr_reprof,'LineWidth',1.5);
ylabel('H');
legend ('H_{pp}','H_{cov}','H_{reprof}','Location','northeast' );
ylim([-.25 .2]);
xlim([amin amax]);
title(['jz'  num2str(jz)  ]);
grid on;
%hold off;

print -djpeg slides\HH_z3.jpg


% REPORT 
pc_cov=val_cov; %(val_cov./val)-1;
pc_reprof=val_reprof; %(val_reprof./val)-1;
pc_pp=val_pp; %(val_pp./val)-1;



% Create a table with the data and variable names

%T = table(aa(ja), pc_cov(ja,1), pc_reprof(ja,1), pc_pp(ja,1), 'VariableNames', { 'a', 'cov', 'reprof', 'pp'} )
T_sovereign = table(zz(1:ja), aa(1:ja), pc_cov(1:ja,1), pc_reprof(1:ja,1), pc_pp(1:ja,1), 'VariableNames', {'z', 'a', 'cov', 'reprof', 'pp'} )

% Write data to text file
%writetable(T, 'slides_dyn\flex_epsshock\amplification.txt')

T_HH = table(zz(1:ja), aa(1:ja), HHpc_cov(1:ja,1), HHpc_reprof(1:ja,1), HHpc_pp(1:ja,1), 'VariableNames', {'z', 'a', 'cov', 'reprof', 'pp'} )
